package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ContractType;
import com.ruoyi.system.service.IContractTypeService;

/**
 * 合同类型信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/contractType")
public class ContractTypeController extends BaseController
{
    @Autowired
    private IContractTypeService contractTypeService;

    /**
     * 获取合同类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:contractType:list')")
    @GetMapping("/list")
    public AjaxResult list(ContractType contractType)
    {
        List<ContractType> contractTypes = contractTypeService.selectContractTypeList(contractType);
        return success(contractTypeService.buildContractTypeTree(contractTypes));
    }

    /**
     * 根据合同类型编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:contractType:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable Long typeId)
    {
        return success(contractTypeService.selectContractTypeByTypeId(typeId));
    }

    /**
     * 获取合同类型下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(ContractType contractType)
    {
        List<ContractType> contractTypes = contractTypeService.selectContractTypeList(contractType);
        return success(contractTypeService.buildContractTypeTree(contractTypes));
    }

    /**
     * 新增合同类型
     */
    @PreAuthorize("@ss.hasPermi('system:contractType:add')")
    @Log(title = "合同类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ContractType contractType)
    {
        if ("1".equals(contractTypeService.checkContractTypeNameUnique(contractType)))
        {
            return error("新增合同类型'" + contractType.getTypeName() + "'失败，合同类型名称已存在");
        }
        contractType.setCreateBy(getUsername());
        return toAjax(contractTypeService.insertContractType(contractType));
    }

    /**
     * 修改合同类型
     */
    @PreAuthorize("@ss.hasPermi('system:contractType:edit')")
    @Log(title = "合同类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ContractType contractType)
    {
        if ("1".equals(contractTypeService.checkContractTypeNameUnique(contractType)))
        {
            return error("修改合同类型'" + contractType.getTypeName() + "'失败，合同类型名称已存在");
        }
        else if (contractType.getParentId().equals(contractType.getTypeId()))
        {
            return error("修改合同类型'" + contractType.getTypeName() + "'失败，上级合同类型不能是自己");
        }
        contractType.setUpdateBy(getUsername());
        return toAjax(contractTypeService.updateContractType(contractType));
    }

    /**
     * 删除合同类型
     */
    @PreAuthorize("@ss.hasPermi('system:contractType:remove')")
    @Log(title = "合同类型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeId}")
    public AjaxResult remove(@PathVariable Long typeId)
    {
        if (contractTypeService.hasChildByContractTypeId(typeId))
        {
            return warn("存在下级合同类型,不允许删除");
        }
        if (contractTypeService.checkContractTypeExistContract(typeId))
        {
            return warn("合同类型存在合同,不允许删除");
        }
        return toAjax(contractTypeService.deleteContractTypeByTypeId(typeId));
    }
}
